﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;


namespace GUI
{
    public partial class uc_GhiMuon : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai bao bien

        DocGia_BLL dg_bll = new DocGia_BLL();
        TextMessageBox box = new TextMessageBox();
        bool isGV = true;
        Reports_BLL rp_bll = new Reports_BLL();
        #endregion


        #region LoadForm
        public uc_GhiMuon()
        {
            InitializeComponent();
        }


        private void uc_GhiMuon_Load(object sender, EventArgs e)
        {
            Search_Data();
        }

        #endregion

        #region Cac chuc nang

        private void btnGhiMuon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmMuonTaiLieu frm = new frmMuonTaiLieu();
            frm.ShowDialog();
        }

        private void btnGiaoVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isGV = true;
            Search_Data();
        }

        private void btnHocVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
            isGV = false;
            Search_Data();
        }

        private void btnXemChiTiet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(isGV==true)
            {
                if (grv_GiaoVien.RowCount > 0)
                {
                    frmXemChiTiet frm = new frmXemChiTiet();
                    frm.maPhieu = grv_GiaoVien.GetRowCellValue(grv_GiaoVien.FocusedRowHandle, colMaPhieuYC).ToString();
                    frm.ShowDialog();
                }
            }
            else
            {
                if (grv_HocVien.RowCount > 0)
                {
                    frmXemChiTiet frm = new frmXemChiTiet();
                    frm.maPhieu = grv_HocVien.GetRowCellValue(grv_HocVien.FocusedRowHandle, colMaPhieuYCHV).ToString();
                    frm.ShowDialog();
                }
            }
        }

        #endregion

        #region ham viet them


        #endregion

        private void btnTimKiem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Search_Data();
        }

        private void Search_Data()
        { 
            PhieuYeuCau_Public pyc = new PhieuYeuCau_Public();
            if (isGV == true)
            {
                gr_HocVien.Hide();
                gr_GiaoVien.Show();
                gr_GiaoVien.Dock = DockStyle.Fill;
               
                pyc.MaDocGia = txtMaBanDoc.Text.Trim();
                pyc.NgayLapPhieu = dtpTuNgay.Value;
                
                grd_GiaoVien.DataSource = dg_bll.Load_GiaoVien(pyc,txtHoTen.Text,dtpDenNgay.Value);
                grv_GiaoVien.BestFitColumns();
            }
            else
            {
                gr_GiaoVien.Hide();
                gr_HocVien.Show();
                gr_HocVien.Dock = DockStyle.Fill;
                
                pyc.MaDocGia = txtMaBanDoc.Text.Trim();
                pyc.NgayLapPhieu = dtpTuNgay.Value;

                grd_HocVien.DataSource = dg_bll.Load_Muon_SinhVien(pyc, txtHoTen.Text, dtpDenNgay.Value);
                grv_HocVien.BestFitColumns();
            }
        }

        private void btnIn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           

            if (isGV == true)
            {
                grv_GiaoVien.CloseEditor();
                grv_GiaoVien.UpdateCurrentRow();
                //lấy danh sách chọn
                string ma = "";
                
                for (int i = 0; i < grv_GiaoVien.RowCount; i++)
                    if (Convert.ToBoolean(grv_GiaoVien.GetRowCellValue(i, colCheck)) == true)
                        if (ma =="")
                        {
                            ma = grv_GiaoVien.GetRowCellValue(i, colMaPhieuYC).ToString();
                        }
                        else
                            ma = ma + "," + grv_GiaoVien.GetRowCellValue(i, colMaPhieuYC).ToString();

                
                    if (ma != "")
                    {
                        DataTable dt = rp_bll.InPhieuMuon_GiaoVien(ma);
                        //gan formula
                        string[] FormulaFiles = new string[0];
                        string[] FormulaFilesValues = new string[0];
                        //
                        //gọi report
                        frmReportViewer frm = new frmReportViewer();
                        frm.data = dt;
                        frm.FormulaFields = FormulaFiles;
                        frm.FormulaFieldsValue = FormulaFilesValues;
                        frm.path = "\\Reports\\" + "rptInPhieuMuon_GiaoVien.rpt";
                        frm.ShowReport();
                    }
                    else
                        XtraMessageBox.Show("Bạn chưa chọn phiếu mượn cần in!", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                grv_HocVien.CloseEditor();
                grv_HocVien.UpdateCurrentRow();
                //lấy danh sách chọn
                string ma = "";

                for (int i = 0; i < grv_HocVien.RowCount; i++)
                    if (Convert.ToBoolean(grv_HocVien.GetRowCellValue(i, colCheckHV)) == true)
                        if (ma == "")
                        {
                            ma= grv_HocVien.GetRowCellValue(i, colMaPhieuYCHV).ToString();
                        }
                        else
                            ma= ma+","+grv_HocVien.GetRowCellValue(i, colMaPhieuYCHV).ToString();
                
               
                if (ma != "")
                {
                    DataTable dt = rp_bll.InPhieuMuon_HocVien(ma);
                    //gan formula
                    string[] FormulaFiles = new string[0];
                    string[] FormulaFilesValues = new string[0];
                    //
                    //gọi report
                    frmReportViewer frm = new frmReportViewer();
                    frm.data = dt;
                    frm.FormulaFields = FormulaFiles;
                    frm.FormulaFieldsValue = FormulaFilesValues;
                    frm.path = "\\Reports\\" + "rptInPhieuMuon_HocVien.rpt";
                    frm.ShowReport();
                }
                else
                    XtraMessageBox.Show("Bạn chưa chọn phiếu mượn cần in!", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnCheckAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (isGV == true)
            {
                for (int i = 0; i < grv_GiaoVien.RowCount; i++)
                    grv_GiaoVien.SetRowCellValue(i, colCheck, true);
            }
            else
            {
                for (int i = 0; i < grv_HocVien.RowCount; i++)
                    grv_HocVien.SetRowCellValue(i, colCheckHV, true);
            }
        }

        private void btnboChon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (isGV == true)
            {
                for (int i = 0; i < grv_GiaoVien.RowCount; i++)
                    grv_GiaoVien.SetRowCellValue(i, colCheck, false);
            }
            else
            {
                for (int i = 0; i < grv_HocVien.RowCount; i++)
                    grv_HocVien.SetRowCellValue(i, colCheckHV, false);
            }
        }
    }
}
